package com.tiza.xiaoyoubao.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by mapple on 2017/5/11.
 */

public class DateUtils {

    //utc转换成时间
    public static String getDateToString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long t = Long.parseLong(time);
        Date date = new Date(t * 1000);
        return sdf.format(date);
    }
    //utc转换成时间
    public static String getDateToStringDay(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        long t = Long.parseLong(time);
        Date date = new Date(t * 1000);
        return sdf.format(date);
    }
    //utc转换成时间
    public static String getDateToStringSec(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = Long.parseLong(time);
        Date date = new Date(t * 1000);
        return sdf.format(date);
    }

    public static String formateRate(String rateStr) {
        if (rateStr.indexOf(".") != -1) {
            //获取小数点的位置
            int num = 0;
            num = rateStr.indexOf(".");
            //获取小数点后面的数字 是否有两位 不足两位补足两位
            String dianAfter = rateStr.substring(0, num + 1);
            String afterData = rateStr.replace(dianAfter, "");
            if (afterData.length() < 2) {
                afterData = afterData + "0";
            } else {
                afterData = afterData;
            }
            String dotBefor = rateStr.substring(0, num);
            if (TextUtils.isEmpty(dotBefor)) {
                dotBefor="0";
            }
            return dotBefor + "." + afterData.substring(0, 2);
        } else {
            return rateStr + ".00";
        }
    }

    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate;
        Date endDate;
        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
            //System.out.println("相隔的天数="+day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 获取当前时间的后一天时间
     * @param cl
     * @return
     */
    private static Calendar getAfterDay(Calendar cl){
        //使用roll方法进行回滚到后一天的时间
        //cl.roll(Calendar.DATE, 1);
        //使用set方法直接设置时间值
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day+1);
        return cl;
    }
    /**
     * 设置时间
     * @param year
     * @param month
     * @param date
     * @return
     */
    public static Calendar setCalendar(int year,int month,int date){
        Calendar cl = Calendar.getInstance();
        cl.set(year, month-1, date);
        return cl;
    }

    /**
     * 获取时间
     * @param date
     * @param num
     * @return
     */
    public static Date nextDate(Date date,int num){
        Calendar cla=Calendar.getInstance();
        cla.setTime(date);
        cla.add(Calendar.DAY_OF_YEAR,num);
        return cla.getTime();
    }
    public static void main(String arg[]) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("结果是：" +getDateToStringSec("1500522770"));

    }

}
